<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartPaymentPlatilka')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

/** 
 *  This class provides platilka payment logic
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
include_spip('functions/pdk_common');
pdk_load_class('AlohacartExtension', 'classes'); 
class AlohacartPaymentPlatilka extends AlohacartExtension {
    public $fields = array('subseller_id', 'api_password', 'id_order_statut_after_payment');
    
    public function __construct() {
        parent::__construct();
        $this->configExtension();
    }
    
    private function configExtension(){
        $this->type = 'payment';
        $this->code = 'platilka';
        $this->isPayable = true;
        
        // Define validation rules:         
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'subseller_id',
                     'label'   => _T('alohacart_payment_platilka:subseller_id'),
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'api_password',
                     'label'   => _T('alohacart_payment_platilka:api_password'),
                     'rules'   => 'required'
                  ),
            )
         );
    }
    
    public function GetData($options=array()) {
        // Try to get id_order from $options array or request
        if ($options['id_order']) $id_order = $options['id_order'];
        if (!$id_order && _request('id_order')) $id_order = _request('id_order');
        if (!$id_order) return '';
        
        $data = $this->GetMetaData();  
        
        $order_mdl = pdk_load_class('AlohacartOrderModel', 'classes/');
        $currency_mdl = pdk_load_class('CurrencyModel','classes/');
        
        $order = $order_mdl->select_by_id($id_order);
        
        // For this system we must convert price into UAH 
        $currency_uah = $currency_mdl->SelectByCode('UAH');        
        if (!$currency_uah) return;
        $total_price = $currency_mdl->ConvertPrice($order['total_price'], $currency_mdl->GetMainCurrencyId()/*$order['id_currency']*/, $currency_uah['id_currency']);
        
        $data['total_price'] = $total_price;      
        $data['id_order'] = $id_order;
        $data['description'] = _T('alohacart_payment_platilka:payment_description', array('nom_site_spip'=>extraire_multi($GLOBALS['meta']['nom_site'])));
              
        return recuperer_fond('inc/payment/alohacart_platilka', $data);
    }
    
    /**
     *  Checks signature of status data
     *  @param array $status_data Status data
     *  @return bool
     */
    public function CheckSignature($status_data) {
        $meta_data = $this->GetMetaData();
        $api_password = $meta_data['api_password'];
        
        $signature = sha1($status_data['transaction_id'].$status_data['subseller_id'].$status_data['order_id'].$status_data['amount'].$status_data['is_test'].
            $status_data['status'].$api_password);
        
        if($status_data['signature'] === $signature) 
            return true;
        else
            return false;
    }
    
    /**
     *  Creates payment text data (info), that can be written into DB orders table
     *  @param array $data Array of data, from wich payment data will be created 
     */
    public function CreatePaymentData($data) {
        $string = '';        
        $string .= isset($data['transaction_id'])&&$data['transaction_id']?_T('alohacart_payment_platilka:transaction_id').': '.$data['transaction_id']."\n":'';
        $string .= isset($data['subseller_id'])&&$data['subseller_id']?_T('alohacart_payment_platilka:subseller_id').': '.$data['subseller_id']."\n":'';
        $string .= isset($data['order_id'])&&$data['order_id']?_T('alohacart_payment_platilka:order_id').': '.$data['order_id']."\n":''; 
        $string .= isset($data['description'])&&$data['description']?_T('alohacart_payment_platilka:description').': '.$data['description']."\n":'';
        $string .= isset($data['status'])&&$data['status']?_T('alohacart_payment_platilka:status').': '.$data['status']."\n":'';
        $string .= isset($data['message'])&&$data['message']?_T('alohacart_payment_platilka:message').': '.$data['message']."\n":'';
        $string .= isset($data['amount'])&&$data['amount']?_T('alohacart_payment_platilka:amount').': '.$data['amount']."\n":'';
        $string .= isset($data['is_test'])&&$data['is_test']?_T('alohacart_payment_platilka:is_test').': '.$data['is_test']."\n":'';        
        return $string;
    }
}
?>